package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.constant.UserConstant;
import com.zh.domain.dto.team.*;
import com.zh.domain.vo.team.TeamUserHistoryVo;
import com.zh.domain.vo.team.TeamUserVo;
import com.zh.domain.vo.user.UserInTeamVo;
import com.zh.exception.ThrowUtils;
import com.zh.service.TeamService;
import com.zh.service.TeamUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 团队接口
 */
@RestController
@RequestMapping("/team")
@Api(tags = "TeamController")
@Slf4j
@SaCheckLogin
public class TeamController {
    @Resource
    private TeamService teamService;
    @Resource
    private TeamUserService teamUserService;

    /**
     * 根据id获取团队信息
     *
     * @return 团队信息
     */
    @GetMapping
    @ApiOperation(value = "getTeamUserVo", notes = "获取我的团队信息")
    @SaCheckLogin
    public BaseResponse<List<TeamUserVo>> getTeamUserVoByUserId() {
        List<TeamUserVo> teamUserVos = teamService.getTeamUserVoByUserId();
        return ResultUtils.success(teamUserVos);
    }

    /**
     * 根据id获取团队成员列表
     *
     * @return 团队成员信息
     */
    @PostMapping("/page")
    @ApiOperation(value = "pageUserInTeam", notes = "获取团队成员列表")
    @SaCheckLogin
    public BaseResponse<Page<UserInTeamVo>> pageUserInTeam(@RequestBody UserInTeamRequest userInTeamRequest) {
        ThrowUtils.throwIf(null == userInTeamRequest, ErrorCode.PARAMS_ERROR);
        Page<UserInTeamVo> page = teamService.pageUserInTeam(userInTeamRequest);
        return ResultUtils.success(page);
    }

    /**
     * 创建团队
     *
     * @param teamAddRequest 团队信息
     * @return 创建结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "addTeam", notes = "新增团队")
    public BaseResponse<Boolean> addTeam(@RequestBody TeamAddRequest teamAddRequest) {
        ThrowUtils.throwIf(null == teamAddRequest, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(StrUtil.isEmpty(teamAddRequest.getName()), ErrorCode.PARAMS_ERROR, "团队名不能为空");
        boolean result = teamService.add(teamAddRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除团队
     *
     * @param teamId 团队id
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "deleteTeam", notes = "删除团队")
    @SaCheckRole(value = {UserConstant.ADMIN, UserConstant.SUPER_ADMIN}, mode = SaMode.OR)
    public BaseResponse<Boolean> deleteTeam(@RequestParam(name = "teamId") Integer teamId) {
        ThrowUtils.throwIf(null == teamId, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.deleteTeam(teamId);
        return ResultUtils.success(result);
    }

    /**
     * 更新团队信息
     *
     * @param teamUpdateRequest 团队信息
     * @return 更新结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "updateTeam", notes = "更新团队信息")
    @SaCheckRole(value = {UserConstant.ADMIN, UserConstant.SUPER_ADMIN}, mode = SaMode.OR)
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest) {
        ThrowUtils.throwIf(null == teamUpdateRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.updateTeam(teamUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 申请加入团队
     *
     * @return 更新结果
     */
    @GetMapping("/application")
    @ApiOperation(value = "application", notes = "更新团队信息")
    public BaseResponse<Boolean> application(Integer teamId) {
        ThrowUtils.throwIf(null == teamId, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.application(teamId);
        return ResultUtils.success(result);
    }

    @PostMapping("/auditApplication")
    @ApiOperation(value = "auditApplication", notes = "审核请求")
    @SaCheckRole(value = {UserConstant.ADMIN})
    public BaseResponse<Boolean> auditApplication(@RequestBody TeamAuditRequest teamAuditRequest) {
        ThrowUtils.throwIf(null == teamAuditRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.auditApplication(teamAuditRequest);
        return ResultUtils.success(result);

    }

    /**
     * 邀请用户加入团队
     *
     * @param teamInviteRequest
     * @return 更新结果
     */
    @PostMapping("/invite")
    @ApiOperation(value = "inviteUser", notes = "邀请用户加入团队,直接邀请")
    @SaCheckRole(value = {UserConstant.ADMIN, UserConstant.SUPER_ADMIN}, mode = SaMode.OR)
    public BaseResponse<Boolean> inviteUser(@RequestBody TeamInviteRequest teamInviteRequest) {
        ThrowUtils.throwIf(null == teamInviteRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.inviteUser(teamInviteRequest);
        return ResultUtils.success(result);
    }

    @PostMapping("/accept-invite")
    @ApiOperation(value = "acceptInvite", notes = "接受邀请")
    public BaseResponse<Boolean> acceptInvite(@RequestBody AcceptInviteRequest acceptInviteRequest) {
        ThrowUtils.throwIf(null == acceptInviteRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.acceptInvite(acceptInviteRequest);
        return ResultUtils.success(result);
    }

    /**
     * 生成短链邀请
     *
     * @param teamId 团队id
     * @return 更新结果
     */
    @GetMapping("/generate-invite")
    @ApiOperation(value = "generateInvite", notes = "邀请用户加入团队,短链邀请")
    @SaCheckRole(value = {UserConstant.ADMIN, UserConstant.SUPER_ADMIN}, mode = SaMode.OR)
    public BaseResponse<String> generateInvite(Integer teamId) {
        ThrowUtils.throwIf(null == teamId, ErrorCode.PARAMS_ERROR);
        String url = teamService.generateInvite(teamId);
        return ResultUtils.success(url);
    }

    @GetMapping("/resolve-invite")
    @ApiOperation(value = "resolveInvite", notes = "解析短链邀请")
    public BaseResponse<String> resolveInvite(String url) {
        ThrowUtils.throwIf(StrUtil.isEmpty(url), ErrorCode.PARAMS_ERROR);
        String result = teamService.resolveInvite(url);
        return ResultUtils.success(result);
    }

    @PostMapping("/kick-out")
    @ApiOperation(value = "kickOut", notes = "踢出团队成员")
    @SaCheckRole(value = UserConstant.ADMIN)
    public BaseResponse<Boolean> kickOut(@RequestBody TeamKickOutRequest teamKickOutRequest) {
        ThrowUtils.throwIf(null == teamKickOutRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = teamService.kickOut(teamKickOutRequest);

        return ResultUtils.success(result);
    }

    @PostMapping("/listTeamUserHistory")
    @ApiOperation(value = "listTeamUserHistory", notes = "获取团队成员变动记录")
    public BaseResponse<Page<TeamUserHistoryVo>> listTeamUserHistory(TeamUserHistoryPageRequest pageRequest) {
        Page<TeamUserHistoryVo> teamUserHistoryVos = teamService.listTeamUserHistory(pageRequest);
        return ResultUtils.success(teamUserHistoryVos);
    }


} 